package com.example.activiti.leave.controller;


import com.example.activiti.common.core.controller.BaseController;
import com.example.activiti.common.core.domain.AjaxResult;
import com.example.activiti.common.core.page.TableDataInfo;
import com.example.activiti.common.utils.SecurityUtils;
import com.example.activiti.leave.domain.WorkflowData;
import com.example.activiti.leave.service.IWorkflowLeaveService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 请假Controller
 *
 * @author danny
 * @date 2020-10-28
 */
@RestController
@RequestMapping("/workflow/leave")
public class WorkflowLeaveController extends BaseController {

    @Resource
    private IWorkflowLeaveService workflowLeaveService;

    /**
     * 查询请假列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WorkflowData workflowData) {
        startPage();
        workflowData.setCreateBy(SecurityUtils.getUsername());
        List<WorkflowData> list = workflowLeaveService.selectWorkflowLeaveAndTaskNameList(workflowData);
        return getDataTable(list);
    }

    /**
     * 查询请假列表
     */
    @GetMapping("/listAll")
    public TableDataInfo listAll(WorkflowData workflowData) {
        startPage();
        List<WorkflowData> list = workflowLeaveService.selectWorkflowLeaveList(workflowData);
        return getDataTable(list);
    }

    /**
     * 获取请假详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(workflowLeaveService.selectWorkflowLeaveById(id));
    }

    /**
     * 获取请假详细信息
     */
    @GetMapping(value = "ByInstanceId/{instanceId}")
    public AjaxResult getInfoByInstanceId(@PathVariable("instanceId") String instanceId) {
        return AjaxResult.success(workflowLeaveService.selectWorkflowLeaveByInstanceId(instanceId));
    }

    /**
     * 新增请假
     */
    @PostMapping
    public AjaxResult add(@RequestBody WorkflowData workflowData) {
        return toAjax(workflowLeaveService.insertWorkflowLeave(workflowData));
    }

    /**
     * 修改请假
     */
    @PutMapping
    public AjaxResult edit(@RequestBody WorkflowData workflowData) {
        return toAjax(workflowLeaveService.insertWorkflowLeave(workflowData));
    }

    /**
     * 删除请假
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(workflowLeaveService.deleteWorkflowLeaveByIds(ids));
    }
}
